package com.lsmw.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsmw.commonutils.R;
import com.lsmw.order.entity.LsOrder;
import com.lsmw.order.mapper.LsOrderMapper;
import com.lsmw.order.service.LsOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 三号男嘉宾
 * @since 2021-03-19
 */
@Slf4j
@Service
public class LsOrderServiceImpl extends ServiceImpl<LsOrderMapper, LsOrder> implements LsOrderService {

    @Override
    public R getOrders() {
        return R.ok().data("orders", this.list());
    }

    @Override
    public R getOrdersByUserId(Long userId) {
        log.info("查询用户id为" + userId + "的所有订单信息");
        List<LsOrder> list = this.list(new LambdaQueryWrapper<LsOrder>().eq(LsOrder::getUserId, userId));
        return list == null || list.size() == 0 ? R.notFound().data("msg", "用户id不存在") : R.ok().data("orders", list);
    }

    @Override
    public R addOrder(LsOrder lsOrder) {
        return this.save(lsOrder) ? R.created() : R.invalidRequest();
    }

    @Override
    public R updateOrder(LsOrder lsOrder) {
        return this.updateById(lsOrder) ?
                R.ok().data("order",this.getById(lsOrder.getOrderNo()))
                :
                R.notFound().data("msg","请检查订单id或联系管理员！");
    }

    @Override
    public R deleteOrderByOrderId(String orderId) {
        LsOrder byId = this.getById(orderId);
        if(byId != null && this.removeById(orderId)){
            return R.ok().data("order",byId);
        }
        return R.invalidRequest().data("msg","请检查订单id或联系管理员");
    }

    @Override
    public R deleteOrderByOrderIds(List<String> orderIds) {
        List<LsOrder> lsOrders = this.listByIds(orderIds);
        if (orderIds.size() == lsOrders.size() && this.removeByIds(orderIds)) {
            return R.ok().data("orders",lsOrders);
        }
        return R.invalidRequest().data("msg","请检查订单id或联系管理员");
    }
}
